import {takeEvery, all, put} from 'redux-saga/effects'
import request from '../../utils/request'
import users from './users'
import auth from './auth'
import {main as mainAction, cart as cartAction} from "../actions/actionCreators";


// generator 函数
function* rootSaga() {


    // yield takeEvery('user-ok', function *(action) {
    //      console.log(action);
    //      const response = yield request('/auth/logon');
    //      console.log(response);
    //      console.log('user-ok');
    //  });


    yield all([
        users.userDemo(),
        auth.login(),
        yield takeEvery('change-tab', function* (data) {
            console.log(data);
            const {callback} = data;
            yield put(mainAction.changeTab('cart'));
            callback();
        }),
        yield takeEvery('request-cart-list', function* (data) {
            const {callback} = data;
            const response = yield request('/mine/shop-carts');
            const {shopCartList} = response.data;

            const newShopCartList = shopCartList.map((item) => {
                let {Product, ProductSku} = item;
                return {
                    id: item.id,
                    productId: item.ProductId,
                    skuId: item.ProductSkuId,
                    goodsTitle: Product.name,
                    colorTitle: '淡青色',
                    price: ProductSku.price,
                    num: item.num,
                };
            });
            callback(newShopCartList);
            yield put(cartAction.getList(newShopCartList));

        })
    ]);
}


export default rootSaga
